package com.macro.mall.demo.nio;


import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class FileChannelDemo {
    public static void main(String[] args) throws IOException {
        File f = new File("/Users/code/L1/demo/mall/happy.properties");
        RandomAccessFile raf = new RandomAccessFile(f, "rw");
        FileChannel channel = raf.getChannel();
        MappedByteBuffer mapped = channel.map(FileChannel.MapMode.READ_WRITE, 0, 30);
        byte[] data = new byte[30];
        int foot =0;
        while (mapped.hasRemaining()) {
            data[foot ++] = mapped.get();
        }
        System.out.println("read content = " + new String(data));
        mapped.clear();
        mapped.put("name=huanghaha".getBytes());
        channel.close();
    }
}
